/**
 * 智慧小区管理系统 - Vite构建配置
 * @author Wu.Liang
 * @date 2024-01-01
 * @updated 2024-12-21 - 优化Token管理和错误处理
 * @updated 2024-12-22 - 适配Sa-Token认证框架
 * @updated 2025-01-30 - 优化代码分割和依赖预构建，解决动态导入问题
 * @updated 2025-01-30 - 修复Vite依赖优化问题，解决chunk-XOHNOAYS.js缺失问题
 * @updated 2025-01-30 - 修复Element Plus组件依赖解析问题
 * @updated 2025-01-30 - 修复Vite依赖预构建问题，解决chunk-LS2NJRQS.js缺失问题
 */
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        AutoImport({
            resolvers: [ElementPlusResolver()],
            imports: ['vue', 'vue-router', 'pinia'],
            dts: 'src/auto-imports.d.ts'
        }),
        Components({
            resolvers: [ElementPlusResolver()],
            dts: 'src/components.d.ts'
        })
    ],
    resolve: {
        alias: {
            '@': resolve(__dirname, 'src'),
            // 修复Element Plus color-picker组件依赖问题
            '@element-plus/components/color-picker-panel/css': 'element-plus/theme-chalk/el-color-picker.css'
        }
    },
    server: {
        port: 3000,
        open: true,
        proxy: {
            '/api': {
                target: 'http://127.0.0.1:8000',
                changeOrigin: true,
                ws: true,
                secure: false
            }
        }
    },
    build: {
        target: 'es2015',
        outDir: 'dist',
        assetsDir: 'assets',
        sourcemap: false,
        rollupOptions: {
            output: {
                chunkFileNames: 'js/[name]-[hash].js',
                entryFileNames: 'js/[name]-[hash].js',
                assetFileNames: '[ext]/[name]-[hash].[ext]',
                manualChunks: {
                    'vendor': ['vue', 'vue-router', 'pinia'],
                    'element-plus': ['element-plus'],
                    'axios': ['axios'],
                    'utils': ['lodash-es', 'dayjs'],
                    'charts': ['echarts', 'vue-echarts']
                }
            }
        },
        // 优化构建性能
        minify: 'terser',
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true
            }
        }
    },
    css: {
        preprocessorOptions: {
            scss: {
                api: 'modern-compiler'
            }
        }
    },
    optimizeDeps: {
        include: [
            'vue', 
            'vue-router', 
            'pinia', 
            'element-plus',
            '@element-plus/icons-vue',
            'axios',
            'lodash-es',
            'dayjs',
            'echarts',
            'vue-echarts'
        ],
        exclude: [
            'vue-demi',
            // 修复Vite依赖预构建问题，排除不兼容的依赖
            'element-plus/es/components/card/style/css',
            'element-plus/es/components/dropdown-item/style/css',
            'element-plus/es/components/avatar/style/css',
            'element-plus/es/components/dropdown/style/css',
            'element-plus/es/components/dropdown-menu/style/css',
            'element-plus/es/components/tooltip/style/css',
            'element-plus/es/components/breadcrumb/style/css',
            'element-plus/es/components/breadcrumb-item/style/css',
            'element-plus/es/components/tag/style/css',
            'element-plus/es/components/scrollbar/style/css',
            'element-plus/es/components/menu/style/css',
            'element-plus/es/components/sub-menu/style/css',
            'element-plus/es/components/menu-item/style/css'
        ],
        // 强制预构建Element Plus相关依赖
        force: true,
        // 添加依赖优化配置
        esbuildOptions: {
            target: 'es2015'
        }
    },
    // 预构建配置
    preview: {
        port: 4173,
        open: true
    }
})
